package com.faithbj.process;

import com.faithbj.common.model.CheckTotal;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * Task DTO, 任务属性均可从此获取.
 * User: xiongyuanwei
 * Date: 12-7-10
 * Time: 下午10:11
 */
public class TaskModel implements Serializable {

    /**
	 * 
	 */
	private static final long serialVersionUID = -3499165868873087358L;
	private long id;
    private String name;
    private String user;
    private Date createdOn;
    private long processInstanceId;
    private Map<String, Object> content;

    public TaskModel() {

    }

    public TaskModel(long id, String name, String user, Date createdOn, long processInstanceId, Map<String, Object> content) {
        this.id = id;
        this.name = name;
        this.user = user;
        this.createdOn = createdOn;
        this.processInstanceId = processInstanceId;
        this.content = content;
    }

    /**
     * 任务id.
     * @return 任务id
     */
    public long getId() {
        return id;
    }

    /**
     * 任务名称.
     * @return 任务名称
     */
    public String getName() {
       return name;
    }

    /**
     * 任务处理人.
     * @return 任务处理人
     */
    public String getUser() {
        return user;
    }

    /**
     * 任务生成时间.
     * @return 任务生成时间
     */
    public Date getCreatedOn() {
        return createdOn;
    }

    public long getProcessInstanceId() {
        return processInstanceId;
    }

    public void setProcessInstanceId(long processInstanceId) {
        this.processInstanceId = processInstanceId;
    }

    /**
     * 任务是否被领取过.
     * @return 任务被领取过则返回true
     */
    public boolean isClaimed() {
        return user != null && !user.trim().isEmpty();
    }

    /**
     * 获取流程变量.
     * @param key 变量key
     * @return 变量值
     */
    public Object get(String key) {
        return getContent().get(key);
    }

    /**
     * 流程模块内部使用, 请不要使用该接口.
     * @return 流程变量表
     */
    @Deprecated
    public Map<String, Object> getContent() {
        return content;
    }

    /**
     * 获取任务中保存的checkTotal.
     * @return CheckTotal
     */
    public CheckTotal getCheckTotal() {
        Map<String, Object> content = getContent();
        if (content != null && content.containsKey("checkTotal")) {
            return (CheckTotal) content.get("checkTotal");
        } else {
            return null;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        TaskModel taskModel = (TaskModel) o;

        if (id != taskModel.id) return false;
        if (processInstanceId != taskModel.processInstanceId) return false;
        if (content != null ? !content.equals(taskModel.content) : taskModel.content != null) return false;
        if (createdOn != null ? !createdOn.equals(taskModel.createdOn) : taskModel.createdOn != null) return false;
        if (name != null ? !name.equals(taskModel.name) : taskModel.name != null) return false;
        if (user != null ? !user.equals(taskModel.user) : taskModel.user != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (id ^ (id >>> 32));
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (user != null ? user.hashCode() : 0);
        result = 31 * result + (createdOn != null ? createdOn.hashCode() : 0);
        result = 31 * result + (int) (processInstanceId ^ (processInstanceId >>> 32));
        result = 31 * result + (content != null ? content.hashCode() : 0);
        return result;
    }
}
